# 第一章. 基础
'''
'''

# 第二章. k-紧邻算法
'''
计算目标样本与数据集的各个维度的绝对差值的和的算数平方根(向量)
|AB| = 根号下((x0-y0)^2 + (x1-y1)^2 + ... + (xn-yn)^2)
'''
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
def create_data_set():
    group = np.array([
        [1.0, 1.1],
        [1.0, 1.0],
        [0, 0],
        [0, 0.1],
    ])
    labels = ['A', 'A', 'B', 'B']
    return group, labels
def In1():
    group, labels = create_data_set()
    knn = KNeighborsClassifier(n_neighbors=1)
    X_train, X_test, y_train, y_test = train_test_split(group, labels)
    knn.fit(X_train, y_train)
    X_new = np.array([[0, 0.2]])
    prediction = knn.predict(X_new)
    print('type is:', prediction[0])

# 识别手写系统
'''
收集数据 准备数据 分析数据 训练算法 测试算法 使用算法
'''
# 1.将图像转换为向量 img2vector
def img2vector(filename):
    return_vect = np.zeros((1, 1024))
    fr = open(filename)
    for i in range(32):
        line_str = fr.readline()
        for j in range(32):
            return_vect[0, 32*i+j] = int[line_str[j]]
    return return_vect